package com.example.apng;

import android.util.LruCache;

import com.linecorp.apng.ApngDrawable;

public class ApngDrawableStore {
    // 计算LruCache缓存大小，大小为最大可用内存的八分之一
    private long maxMemory = Runtime.getRuntime().maxMemory() >> 10;
    private int cacheSize = (int) (maxMemory >> 3);
    private LruCache<String, ApngDrawable> cache = new LruCache<String, ApngDrawable>(cacheSize) {
        @Override
        protected int sizeOf(String key, ApngDrawable value) {
            return super.sizeOf(key, value);
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, ApngDrawable oldValue, ApngDrawable newValue) {
            if (evicted) {
                remove(key);
            }
        }

    };

    public ApngDrawable get(String key) {
        return cache.get(key);
    }

    public void put(String key, ApngDrawable apngDrawable) {
        cache.put(key, apngDrawable);
    }

    public void remove(String key) {
        ApngDrawable apngDrawable = cache.remove(key);
        if (apngDrawable != null) {
            apngDrawable.recycle();
        }
    }

}
